//
//  WaitReceiveViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/11.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "WaitReceiveViewController.h"
#import "OrderSubCell.h"
#import "OrderCenterStore.h"
#import "OrderCenterParametersModel.h"
#import "OrderCenterList.h"

@interface WaitReceiveViewController ()<UITableViewDelegate,UITableViewDataSource,OrderSubCellDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) OrderCenterParametersModel *parametersModel;
@property (nonatomic,strong) NSMutableArray *listArr;

@end

@implementation WaitReceiveViewController

- (OrderCenterParametersModel *)parametersModel{
    
    if (!_parametersModel) {
        
        _parametersModel = [[OrderCenterParametersModel alloc] init];
        _parametersModel.isPay = @"2";
        _parametersModel.flag = @"2";
        _parametersModel.customId = [UserDefaultsManager sharedInstance].user_id;
    }
    
    return _parametersModel;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.emptyDataSetDelegate = self;
//        _tableView.emptyDataSetSource = self;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.sectionHeaderHeight = 9.5;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(0);
        
        make.bottom.equalTo(self.view).offset(-45);
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)refresh{
    
    OrderCenterStore *store = [[OrderCenterStore alloc] init];
    
    [store getOrderListWithParametersModel:self.parametersModel Success:^(NSArray *listArr) {
        
        [self.tableView.mj_header endRefreshing];
        
        self.listArr = [NSMutableArray arrayWithArray:listArr];
        
        [self.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [OrderSubCell getHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OrderSubCell *cell = [OrderSubCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.type = 2;
    [cell setAlphaWithType:2];
    cell.model = self.listArr[indexPath.section];
    cell.indexPath = indexPath;
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//    [OrderCenterList sharedInstance].selectedModel = self.listArr[indexPath.section];
//    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(onCurrentCell)]) {
//        
//        [self.delegate onCurrentCell];
//    }
//    
//}

#pragma mark - OrderSubCellDelegate
- (void)onLookWuLiuBtnWithIndexPath:(NSIndexPath *)indexPath{
    
    DLog(@"查看物流");

    [OrderCenterList sharedInstance].selectedModel = self.listArr[indexPath.section];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(lookLogistics)]) {
        
        [self.delegate lookLogistics];
    }
    
}

- (void)onReceiveGoodsBtnWithIndexPath:(NSIndexPath *)indexPath{
    
    DLog(@"确认收货");
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您正在确认收货" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        OrderCenterModel *model = self.listArr[indexPath.section];
        OrderCenterStore *store = [[OrderCenterStore alloc] init];
        MJWeakSelf
        [store onSureReciveGoodsWithId:model.uid Success:^{
            
            [weakSelf.listArr removeObjectAtIndex:indexPath.section];
            
            [weakSelf.tableView beginUpdates];
            
            [weakSelf.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationLeft];
            
            [weakSelf.tableView endUpdates];
            
            [weakSelf showMBPError:@"确认成功！"];
            
            [weakSelf.tableView reloadData];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"hadSureReciveGoods" object:nil];
            
        } Failure:^(NSError *error) {
            
            [weakSelf showMBPError:[HttpTool handleError:error]];
        }];
    }]];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
}

@end
